<template>
  <div
    class="bannerContain"
    @mouseenter="stopInterval"
    @mouseleave="startInterval"
  >
    <img :src="images[currentIndex]" />

    <button class="prev" @click="prev">‹</button>

    <button class="next" @click="next">›</button>

    <div class="dot">
      <span
        v-for="(image, index) in images"
        :key="image"
        :class="{ active: currentIndex === index }"
      ></span>
    </div>
  </div>
</template>

<script>
import img1 from "./assets/1.png";
import img2 from "./assets/2.png";
import img3 from "./assets/3.png";
import img4 from "./assets/4.png";
import img5 from "./assets/5.png";
export default {
  data() {
    return {
      images: [img1, img2, img3, img4, img5],
      currentIndex: 0,
      timer: null,
    };
  },
  methods: {
    prev() {
      this.currentIndex =
        (this.currentIndex - 1 + this.images.length) % this.images.length;
    },
    next() {
      this.currentIndex = (this.currentIndex + 1) % this.images.length;
    },

    startInterval() {
      if (!this.timer) {
        this.timer = setInterval(() => {
          this.next();
        }, 2000);
      }
    },

    stopInterval() {
      if (this.timer) {
        clearInterval(this.timer);
        this.timer = null;
      }
    },
  },
  mounted() {
    this.startInterval();
  },
};
</script>

<style scoped>
.bannerContain {
  position: relative;
  width: 550px;
  height: 400px;
  margin: 0 auto;
}

img {
  width: 550px;
  height: 400px;
}

button {
  position: absolute;
  width: 30px;
  height: 50px;
  top: 50%;
  margin-top: -25px;
  background: rgba(0, 0, 0, 0.5);
  color: white;
  border: none;
  font-size: 30px;
}

.prev {
  left: 1px;
}

.next {
  right: 0px;
}

.dot {
  position: absolute;
  width: 90px;
  height: 20px;
  bottom: 0px;
  left: 50%;
  margin-left: -45px;
}

.dot span {
  display: inline-block;
  width: 8px;
  height: 8px;
  border-radius: 50%;
  background-color: rgba(255, 255, 255, 0.5);
  margin: 0 5px;
}

.dot span.active {
  background: white;
}
</style>